/* 完整代码 */
const yargs = require('yargs');
const requireDirectory = require('require-directory');
const cmdModule = require('./command');
const cmds = requireDirectory(cmdModule, {
	rename: (path) => { // a-b 转 aB
		return path.replace(/([\-\_\.]+[\w\W]{1})/ig, (regPart) => {
			return regPart.charAt(1).toUpperCase();
		});
	}
});

Object.keys(cmds).forEach((cmdKey) => {
	if(cmdKey.slice(0, 2) == 'aa') return;
	const cmdObj = cmds[cmdKey];
	cmdObj.builder = () => {
		if (cmdObj.options) {
			// 对子命令参数设置分组
			Object.keys(cmdObj.options).forEach((optKey) => {
				cmdObj.options[optKey].group = cmdKey;
			});
			yargs.options(cmdObj.options);
		}
	};
	yargs.command(cmdObj);
});
// yargs.command('$0', '', ()=>{}, ()=>{
// 	yargs.showHelp(); // 显示帮助文档
// });
const shellArgs = yargs.options({
	'cwd': {
		default: './',
		describe: '执行的目录',
		type: 'string',
		group: '公共参数'
	},
}).argv;
shellArgs._.length || yargs.showHelp();
//**/

/* yargs基础用法：配置参数 *
const yargs = require('yargs');
yargs.command('$0', '脚本介绍信息这里显示', () => {}, (argv) => {
    console.info('command', argv);
});
const commandArgs = yargs.options({
	'cache': { alias: ['c'], default: false, describe: '是否启用缓存', type: 'boolean' }
}).argv;
//*/

/* yargs基础用法：多指令配置 *
const yargs = require('yargs');
yargs.command('release', '脚本介绍信息这里显示', ()=>{
	yargs.options({
		'watch': { alias: ['w'], default: false, describe: '是否持续监听', type: 'boolean', group: 'release'}
	});
}, (argv) => {
	console.log(argv);
});
yargs.command('build', '脚本介绍信息这里显示', ()=>{
	yargs.options({
		'cache': { alias: ['c'], default: false, describe: '是否启用缓存', type: 'boolean', group: 'build'}
	});
}, (argv) => {
	console.log(argv);
});
const commandArgs = yargs.options({
	'cmd': { default: __dirname, describe: '执行路径', type: 'boolean' }
}).argv;
//*/

/* require-directory基础用法：检测目录，变更命名 *
const requireDirectory = require('require-directory');
const cmdModule = require('./command');
const cmdsOption = requireDirectory(cmdModule, {
	visit: (obj) => {
		// 可以添加属性和方法 
		return obj;
	},
	rename: (path) => {
		// a-b 转 aB
		// console.log('rename', path);
		path = path.replace(/([\-\_\.]+[\w\W]{1})/ig,(regPart) => {
			return regPart.charAt(1).toUpperCase();
		});
		return path;
	}
});
console.log(cmdsOption);
//*/
